package cn.net.isir.service2;

import cn.net.isir.dao.MinipIndexDataDao;
import cn.net.isir.dao.TotalFlowStatisticsDao;
import cn.net.isir.entity.TimeRange;
import cn.net.isir.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author liuxubo
 * @filename TotalFlowStatisticsService.java
 * @package cn.net.isir.service
 * @email lxb@isir.net.cn
 * @date 2019/03/08
 * @comment
 * @since 1.8
 */
@Service
public class TotalFlowStatisticsService {
    @Resource
    private TotalFlowStatisticsDao totalFlowStatisticsDao;
    @Resource
    private MinipIndexDataDao minipIndexDataDao;

    /**
     * 查询总统计数据
     *
     * @param salesman 销售人员
     * @return 结果
     */
    public Map<String, BigDecimal> getTotalStatistics(Map<String, List<TimeRange>> salesman, String turnedShopIds) {
        Map<String, BigDecimal> map = null;
        if(!salesman.isEmpty()){
            map= new HashMap<>();
            map.put("day", totalFlowStatisticsDao.queryDayTotalFlow(salesman, turnedShopIds));
            map.put("month", totalFlowStatisticsDao.queryMonthTotalFlow(salesman, turnedShopIds));
            map.put("quarter", minipIndexDataDao.queryIntervalFlow(salesman, turnedShopIds, DateUtils.getCurrentQuarterStartTime(), DateUtils.getCurrentQuarterEndTime()));
            BigDecimal yearTotal = minipIndexDataDao.queryIntervalFlow(salesman, turnedShopIds, DateUtils.getCurrentYearStartTime(), DateUtils.getCurrentYearEndTime());
            map.put("year",yearTotal);
            return map;
        }
       return new HashMap<>();
    }

    /**
     * 查询当月每日的流水
     *
     * @return 结果
     */
    public Map<String, BigDecimal> getDaysFlowAtMonth(Map<String, List<TimeRange>> salesman, String turnedShopIds) {
        Calendar calendar = Calendar.getInstance();
        int maxDate = calendar.getActualMaximum(Calendar.DATE);
        String startTime = new SimpleDateFormat("yyyy-MM-## 00:00:00").format(calendar.getTime());
        String endTime = new SimpleDateFormat("yyyy-MM-## 23:59:59").format(calendar.getTime());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取每一天的数据
        Map<String, BigDecimal> dateMap = new LinkedHashMap<>();
        for (int i = 1; i <= maxDate; i++) {
            String dateStr = String.valueOf(i);
            if (i < 10) {
                dateStr = "0" + dateStr;
            }
            String realStime = startTime.replace("##", dateStr);
            String realEtime = endTime.replace("##", dateStr);
            try {
                Date sDate = sdf.parse(realStime);
                Date eDate = sdf.parse(realEtime);
                dateMap.put(dateStr, minipIndexDataDao.queryIntervalFlow(salesman, turnedShopIds, sDate, eDate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return dateMap;
    }

    /**
     * 查询当年每月的流水
     *
     * @return 结果
     */
    public Map<String, BigDecimal> getMonthsFlowAtYear(Map<String, List<TimeRange>> salesman, String turnedShopIds) {
        Calendar calendar = Calendar.getInstance();
        String startTime = new SimpleDateFormat("yyyy-##-01 00:00:00").format(calendar.getTime());
        String endTime = new SimpleDateFormat("yyyy-##-#2#2 23:59:59").format(calendar.getTime());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取每月的数据
        Map<String, BigDecimal> dateMap = new LinkedHashMap<>();
        for (int i = 1; i <= 12; i++) {
            String dateStr = String.valueOf(i);
            if (i < 10) {
                dateStr = "0" + dateStr;
            }
            String realStime = startTime.replace("##", dateStr);
            String realEtime = endTime.replace("##", dateStr);
            // 计算本月的做大日期
            calendar.set(Calendar.MONTH, i - 1);
            int maxDate = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            realEtime = realEtime.replace("#2#2", String.valueOf(maxDate));

            try {
                Date sDate = sdf.parse(realStime);
                Date eDate = sdf.parse(realEtime);
                BigDecimal monthFlow = minipIndexDataDao.queryIntervalFlow(salesman, turnedShopIds, sDate, eDate);
                dateMap.put(dateStr, monthFlow);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return dateMap;
    }

    /**
     * 查询年每季度的流水
     *
     * @return 结果
     */
    public Map<String, BigDecimal> getQuarterFlowAtYear(Map<String, List<TimeRange>> salesman, String turnedShopIds) {
        Calendar calendar = Calendar.getInstance();
        String startTime = new SimpleDateFormat("yyyy-##-01 00:00:00").format(calendar.getTime());
        String endTime = new SimpleDateFormat("yyyy-##-#2#2 23:59:59").format(calendar.getTime());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取每月的数据
        Map<String, BigDecimal> dateMap = new LinkedHashMap<>();
        for (int i = 1; i <= 12; i += 3) {
            String dateStr = String.valueOf(i);
            String dateStr2 = String.valueOf(i + 2);
            if (i < 10) {
                dateStr = "0" + dateStr;
            }
            if (i + 2 < 10) {
                dateStr2 = "0" + dateStr2;
            }
            String realStime = startTime.replace("##", dateStr);
            String realEtime = endTime.replace("##", dateStr2);
            // 计算本月的做大日期
            calendar.set(Calendar.MONTH, i + 2 - 1);
            int maxDate = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            realEtime = realEtime.replace("#2#2", String.valueOf(maxDate));

            try {
                Date sDate = sdf.parse(realStime);
                Date eDate = sdf.parse(realEtime);
                dateMap.put(dateStr + "-" + dateStr2, minipIndexDataDao.queryIntervalFlow(salesman, turnedShopIds, sDate, eDate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return dateMap;
    }

    public static void main(String[] args) {
        System.out.println(Calendar.getInstance().getActualMaximum(Calendar.DATE));
        System.out.println(Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH));
        System.out.println(new SimpleDateFormat("yyyy-MM-## 00:00:00").format(Calendar.getInstance().getTime()));
        try {
            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-02-31 00:11:22"));

            Calendar c = Calendar.getInstance();
            c.set(Calendar.MONTH, Calendar.DECEMBER);
            System.out.println(c.getActualMaximum(Calendar.DAY_OF_MONTH));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


}
